package com.sohoometer.rsinterface;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.session.IoSessionConfig;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.serial.SerialAddress;
import org.apache.mina.transport.serial.SerialAddress.DataBits;
import org.apache.mina.transport.serial.SerialAddress.FlowControl;
import org.apache.mina.transport.serial.SerialAddress.Parity;
import org.apache.mina.transport.serial.SerialAddress.StopBits;
import org.apache.mina.transport.serial.SerialConnector;
import org.apache.mina.transport.serial.SerialSessionConfig;

import com.sohoometer.netpackage.CodecFactory;
import com.sohoometer.netpackage.Handler;

public class RS232Test {
	public static void main(String[] args) throws Exception {
		SerialConnector connector = new SerialConnector();
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CodecFactory()));
		connector.setHandler(new Handler());
//		SerialAddress serialAddress = new SerialAddress("COM1", 38400, StopBits.BITS_1, Parity.NONE, FlowControl.NONE);
		SerialAddress serialAddress = new SerialAddress("COM1", 9600, DataBits.DATABITS_8, StopBits.BITS_1,Parity.NONE, FlowControl.NONE);
		IoSessionConfig sessionConfig = connector.getSessionConfig();
		SerialSessionConfig serialSessionConfig = (SerialSessionConfig) sessionConfig;
		serialSessionConfig.setReceiveThreshold(18);
		System.out.println(sessionConfig);
//		IoSessionConfig sessionConfig = connector.getSessionConfig();
//		sessionConfig.setReadBufferSize(2048);
//		sessionConfig.setIdleTime(IdleStatus.BOTH_IDLE, 1);
		ConnectFuture connectFuture = connector.connect(serialAddress);
	}

}
